<div class="feeds form">
<?php echo $form->create('Feed');?>
	<fieldset>
 		<legend><?php __('Edit Feed');?></legend>
	<?php
		echo $form->input('id');
		echo $form->input('name');
		echo $form->input('Category');
		echo $form->input('url', array('label'=>'Url ['.$html->link(__('Preview', true), array('action'=>'preview'), array('id'=>'previewDialogOpen')).']'));
		echo $form->input('match_field');
		echo $form->input('download_search');
		echo $form->input('download_replace', array('label'=>'Download Replace ['.$html->link(__('Preview', true), array('action'=>'checkReplace'), array('id'=>'replaceDialogOpen')).']'));
		echo $form->input('download_regex', array('label'=>'Download Regex ['.$html->link(__('Preview', true), array('action'=>'checkRegex'), array('id'=>'regexDialogOpen')).']'));
		echo $form->input('download_xpath', array('label'=>'Download Xpath ['.$html->link(__('Preview', true), array('action'=>'checkXPath'), array('id'=>'xpathDialogOpen')).'] &nbsp;['.$html->link(__('Find', true), array('action'=>'findXPath'), array('id'=>'xpathFindOpen')).']'));
		echo $form->input('url_pattern');
		echo $form->input('Favorite');
	?>
	</fieldset>
<?php echo $form->end('Submit');?>
</div>
<div class="actions">
	<ul>
		<li><?php echo $html->link(__('Delete', true), array('action'=>'delete', $form->value('Feed.id')), null, sprintf(__('Are you sure you want to delete # %s?', true), $form->value('Feed.id'))); ?></li>
		<li><?php echo $html->link(__('List Feeds', true), array('action'=>'index'));?></li>
		<li><?php echo $html->link(__('List Favorites', true), array('controller'=> 'favorites', 'action'=>'index')); ?> </li>
		<li><?php echo $html->link(__('New Favorite', true), array('controller'=> 'favorites', 'action'=>'add')); ?> </li>
	</ul>
</div>


<script>
	$(document).ready(function() {
		$('#previewDialogOpen').click(function(){
			$('<div id="previewDialog"></div>')
				.appendTo('body')
				.hide()
				.load(this.href+'/?feedurl='+escape($('#FeedUrl').val()),{},function(){
					$(this)
					.dialog({
						'title': $('h1',this).hide().text(),
						'width': 300,	
						'height': 160, 
						'modal': true
					})
				})
				;
			return false;
		});
		
		$('#xpathDialogOpen').click(function(){
			$('<div id="xpathDialog"></div>')
				.appendTo('body')
				.hide()
				.load(this.href+'/?xpath='+escape($('#FeedDownloadXpath').val())
						+'&feedurl='+escape($('#FeedUrl').val())
						+'&urlpattern='+escape($('#FeedUrlPattern').val() || '#HREF#')
						,{},function(){
					$(this)
					.dialog({
						'title': $('h1',this).hide().text(),
						'width': 300,	
						'height': 160, 
						'modal': true
					})
				})
				;
			return false;
		});

		$('#xpathFindOpen').click(function(){
			var that = this;
			$('<div id="xpathFind"></div>')
				.appendTo('body')
				.hide()
				.load(this.href+'/?feedurl='+escape($('#FeedUrl').val())
						,{},function(text){
					$(this)
					.find('a')
						.click(function(){
							var xpath = '/'+this.tagName.toLowerCase(), hasId = false, sibs;
								$(this).parents().andSelf().each(function(){
									var elxp = ''
									if($(this).attr('id') == 'xpathFind') {
										return false;
									} else {
										sibs = $(this).parent().find('> '+this.tagName);
										if(this.id) { // use id and halt
											elxp = "//*[@id=\""+this.id+"\"]";
											xpath = elxp+xpath;
											hasId = true;
											return false;
										} else if(sibs.length>1) { //siblings
											elxp = '/'+this.tagName.toLowerCase()+'['+(sibs.index(this)+1)+']';
										} else {
											elxp = '/'+this.tagName.toLowerCase();
										}
										xpath = elxp+xpath;
									}
								})
							if(!hasId) xpath = '/html/body'+xpath;
							// alert(xpath);
							$('#FeedDownloadXpath').val(xpath);
							$('#xpathFind').dialog('close');
							return false;
						})
				})
				.dialog({
					'title': 'Find Xpath for '+$('#FeedUrl').val(),
					'width': '90%',	
					'modal': true
				})

				;
			return false;
		});


		$('#regexDialogOpen').click(function(){
			$('<div id="regexDialog"></div>')
				.appendTo('body')
				.hide()
				.load(this.href, {
					regex:escape($('#FeedDownloadRegex').val().split('+').join('%2B')),
					feedurl:escape($('#FeedUrl').val()),
					urlpattern:escape($('#FeedUrlPattern').val() || '#HREF#')
					},function(){
					$(this)
					.dialog({
						'title': $('h1',this).hide().text(),
						'width': 300,	
						'height': 160, 
						'modal': true
					})
				})
				;
			return false;
		});

		$('#replaceDialogOpen').click(function(){
			$('<div id="replaceDialog"></div>')
				.appendTo('body')
				.hide()
				.dialog({
						'width': 300,	
						'height': 160, 
						'modal': true,
					})
				.load(this.href, {
					search:escape($('#FeedDownloadSearch').val().split('+').join('%2B')),
					replace:escape($('#FeedDownloadReplace').val().split('+').join('%2B')),
					feedurl:escape($('#FeedUrl').val())
				})
				;
			return false;
		});

		
	});
</script>